package com.uchiha.nova.mainService.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.core.tool.constant.NovaCRUDMethodConstant;
import com.uchiha.nova.core.model.entity.ServiceEntity;
import com.uchiha.nova.core.model.vo.RoleSelectVO;
import com.uchiha.nova.core.model.vo.ServiceVO;
import com.uchiha.nova.core.model.query.ServiceQuery;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.support.BaseController;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.support.QueryContext;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.service.IServiceService;
import com.uchiha.nova.mainService.wrapper.ServiceWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * controller表  控制器
 *
 * @author magic
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service")
@Tag(name = "controller表 ")
@Slf4j
public class ServiceController extends BaseController {

   @Resource
   IServiceService serviceService;

	/**
	 * 详情
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_DETAIL)
	@Operation(summary = "详情")
	public R<ServiceVO> detail(@RequestBody ServiceEntity serviceEntity) {
		ServiceEntity detail = serviceService.getOne(Condition.getQueryWrapper(serviceEntity));
		return R.data(ServiceWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping(NovaCRUDMethodConstant.NOVA_LIST)
    @Operation(summary = "分页查询")
	public R getPage(ServiceQuery query, QueryContext queryContext) {
		IPage<ServiceEntity> pages = serviceService.page(Condition.getPage(queryContext), Condition.getQueryWrapper(query, queryContext,ServiceEntity.class));
		return R.data(ServiceWrapper.build().pageVO(pages));
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVE)
	@Operation(summary = "新增")
	public R save(@RequestBody ServiceEntity serviceEntity) {
		return R.status(serviceService.save(serviceEntity));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_UPDATE)
	@Operation(summary = "修改")
	public R update(@RequestBody ServiceEntity serviceEntity) {
		return R.status(serviceService.updateById(serviceEntity));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_SAVEORUPDATE)
	@Operation(summary = "新增或修改")
	public R saveOrUpdate(@RequestBody ServiceEntity serviceEntity) {
		return R.status(serviceService.saveOrUpdate(serviceEntity));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping(NovaCRUDMethodConstant.NOVA_REMOVE)
	@Operation(summary = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.status(serviceService.deleteLogic(Func.toLongList(ids)));
	}
  
    /**
	 * 物理删除，不可恢复
	 */
   @Operation(summary = "物理删除")
   @DeleteMapping(NovaCRUDMethodConstant.NOVA_DELETE)
   public R delete(@RequestParam String ids){
     return R.status(serviceService.deletePhysics(Func.toLongList(ids)));
   }

	@GetMapping("/getTree")
	public R getTree(Long parentId){
		return R.data(serviceService.getTree(parentId));
	}

	@GetMapping("/treeAll")
	public R treeAll(){
		return R.data(serviceService.getTreeALl());
	}

	@PostMapping("/roleSelect")
	public R roleSelect(@RequestBody RoleSelectVO roleSelectVO){
		return R.status(serviceService.saveRoleSelectVO(roleSelectVO));
	}

	@GetMapping("/roleHasSelect")
	public R roleHasSelect(Long roleId){
		return R.data(serviceService.getRoleHasSelect(roleId));
	}
}
